﻿
using System;
using System.Collections.Generic;
using System.Linq;
namespace DotNetNeural.Scaling
{
    public class LinearScale : IScale
    {
        public float LeftBorder { get; private set; }

        public float RightBorder { get; private set; }

        public float BorderDelta { get; private set; }

        public LinearScale(float leftBorder, float rightBorder)
        {
            if (LeftBorder > RightBorder)
                throw new ArgumentException(@"Illegal borders of linear scale. 
                                              Left border must be smaller then right one");

            LeftBorder = leftBorder;
            RightBorder = rightBorder;
            BorderDelta = RightBorder - LeftBorder;
        }

        #region IScale Members

        public IEnumerable<float[]> Scale(IEnumerable<float[]> inputs)
        {
            if (inputs == null)
                throw new NullReferenceException("Illegal null-reference inputs");

            if (inputs.Count() == 0)
                return new float[][] { };

            var maxInput = inputs.Max((i) => i.Max());
            var minInput = inputs.Min((i) => i.Min());
            var deviation = maxInput - minInput;

            List<float[]> normalizedInputs = new List<float[]>();
            int size = -1;

            foreach (var inp in inputs)
            {
                size = inp.Length;

                float[] normInp = new float[size];
                for (int i = 0; i < size; ++i)
                {
                    normInp[i] = (inp[i] - minInput) * (BorderDelta) / (deviation) + LeftBorder;
                }

                normalizedInputs.Add(normInp);
            }

            return normalizedInputs;
        }

        #endregion

        #region Private methods

        #endregion
    }
}
